/*-----------------------------------------------------------------------------
	The contents of this file are subject to the Mozilla Public License
	Version 1.1 (the "License"); you may not use this file except in
	compliance with the License.You may obtain a copy of the License at
	http://www.mozilla.org/MPL/MPL-1.1.html
	
	Software distributed under the License is distributed on an "AS IS"
	basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
	License for the specific language governing rights and limitations
	under the License.
	
	The Original Code is Fever Framework code.
	
	The Initial Developer of the Original Code is Romain Ecarnot.
	Portions created by Initial Developer are Copyright (C) 2006
	the Initial Developer. All Rights Reserved.
	
	Contributor(s):
	
	Unless required by applicable law or agreed to in writing, software
	distributed under the License is distributed on an "AS IS" BASIS,
	WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
	See the License for the specific language governing permissions and
	limitations under the License.
-----------------------------------------------------------------------------*/

import fever.data.encoder.IEncoder;

/**
 * <strong>Base64</strong> string encoder.
 * 
 * <p>Orignal ActionScript 2.0 implementation by Mika Palmu in his 
 * AsCrypt library, available in http://www.meychi.com/
 * 
 * @author Mika Palmu
 * @author Romain Ecarnot
 */
class fever.data.encoder.Base64 implements IEncoder
{
	//-------------------------------------------------------------------------
	// Private properties
	//-------------------------------------------------------------------------
	
	private static var base64chars : String = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=";
	private static var _instance : IEncoder;
	
	
	//-------------------------------------------------------------------------
	// Public API
	//-------------------------------------------------------------------------
	
	/**
	 * Returns the Base64 encoder / decoder
	 */
	public static function getProcess() : IEncoder
	{
		if( !_instance ) _instance = new Base64();
		return _instance;
	}
	
	/**
	 * Encodes passed-in {@code value} string.
	 */
	public function encode( value ) : String
	{
		var src : String = value.toString();
		
		var i : Number = 0;
		var output : String = new String("");
		var chr1 : Number, chr2 : Number, chr3 : Number;
		var enc1 : Number, enc2 : Number, enc3 : Number, enc4 : Number;
		
		while (i < src.length)
		{
			chr1 = src.charCodeAt(i++);
			chr2 = src.charCodeAt(i++);
			chr3 = src.charCodeAt(i++);
			enc1 = chr1 >> 2;
			enc2 = ((chr1 & 3) << 4) | (chr2 >> 4);
			enc3 = ((chr2 & 15) << 2) | (chr3 >> 6);
			enc4 = chr3 & 63;
			if(isNaN(chr2)) enc3 = enc4 = 64;
			else if(isNaN(chr3)) enc4 = 64;
			output += base64chars.charAt(enc1)+base64chars.charAt(enc2);
			output += base64chars.charAt(enc3)+base64chars.charAt(enc4);
		}
		return output;
	}
	
	/**
	 * Decodes passed-in {@code source} string.
	 */
	public function decode( value ) : String
	{
		var src : String = value.toString();
		
		var i : Number = 0;
		var output : String = new String("");
		var chr1 : Number, chr2 : Number, chr3 : Number;
		var enc1 : Number, enc2 : Number, enc3 : Number, enc4 : Number;
		while (i < src.length)
		{
			enc1 = base64chars.indexOf(src.charAt(i++));
			enc2 = base64chars.indexOf(src.charAt(i++));
			enc3 = base64chars.indexOf(src.charAt(i++));
			enc4 = base64chars.indexOf(src.charAt(i++));
			chr1 = (enc1 << 2) | (enc2 >> 4);
			chr2 = ((enc2 & 15) << 4) | (enc3 >> 2);
			chr3 = ((enc3 & 3) << 6) | enc4;
			output += String.fromCharCode(chr1);
			if (enc3 != 64) output = output+String.fromCharCode(chr2);
			if (enc4 != 64) output = output+String.fromCharCode(chr3);
		}
		return output;
	}
	
	
	//-------------------------------------------------------------------------
	// Private implementation
	//-------------------------------------------------------------------------
	
	/**
	 * Constructor.
	 */
	private function Base64()
	{
		
	}	
}